package tree.bean;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.HashSet;
//import java.util.Iterator;
import java.util.List;

import example.model.Request;
import tree.model.*;

public class TreeBean {
	
	TreeNode treeIds = new TreeNode();
	
	public TreeBean(){

		  TreeNode n8 = new TreeNode(8);
		  TreeNode n9 = new TreeNode(9);
		  //TreeNode n10 = new TreeNode(10);
		  TreeNode n11 = new TreeNode(11);
		  TreeNode n12 = new TreeNode(12);
		  TreeNode n13 = new TreeNode(13);
		  TreeNode n14 = new TreeNode(14);
		  TreeNode n15 = new TreeNode(15);
		  TreeNode n16 = new TreeNode(16);
		  TreeNode n17 = new TreeNode(17);		  
		  TreeNode n18 = new TreeNode(18);
		  
		  n8.add(n9);
		  	n9.add(n15);
		  		n15.add(n16);
		  	n9.add(n17);
		  n8.add(n11);
		  n8.add(n12);
		  	n12.add(n13);
		  	n12.add(n14);
		  n8.add(n18);
		  treeIds = n8;
		  
		  /*
		   8
		   9		11	12		18
		   15	17		13	14
		   16 
		   */
	}
	
	public List<String> getLevelCount(){
		int depth =  treeIds.fullDepth(treeIds);
		
		List<String> levelList=new ArrayList<String>();
		 for(int i = 1; i<=depth; i++){
			 levelList.add(Integer.toString(i));
		 }				
		return levelList;
	}
	
	public List<String> getLevelItem(int lev){
		List<String> listNodeId = new ArrayList<String>();
		listNodeId.addAll(treeIds.NodesAtLevel(lev));
		return listNodeId;		
	}
	 
	 public Request getNode(int id){
		 Connection con = null;		 
		 Request request = new Request("", "", "", "", "", "", "", "");
		  try {
		   String url = "jdbc:mysql://localhost:3306/dbagatha";
		   con = DriverManager.getConnection(url, "root","12345");
		   Statement st = con.createStatement();		   
		   ResultSet rsF = st.executeQuery("select * from request, project, auther where (request.idparent IS NULL) and (request.idproject = project.id) and (request.idauther = auther.id)");
		   while (rsF.next()) {
			   request.setId(Integer.toString(id));
			   request.setName(rsF.getString(3).toString());
			   request.setCode(rsF.getString(2).toString());
			   request.setStatus(rsF.getString(5).toString());
			   request.setpart(rsF.getString(8).toString());	
			   request.setAuthorName(rsF.getString(18));
		   }
		  } catch (Exception e) {
		   e.printStackTrace();
		   System.out.println("Exception: " + e.getMessage());
		  } finally {
		   try {
		    if (con != null)
		     con.close();
		   } catch (SQLException e) {
		    
		   }
		  }		 		 
		return request;		 
	 }
	 
	 public List<Request> NodesAtLevel(String level){
		 int lev = Integer.parseInt(level);
		 List<String> listNodeId = new ArrayList<String>();
		 listNodeId = getLevelItem(lev);
		 
		 List<Request> listNodeInfo = new ArrayList<Request>();		 
		 
		 Request request = new Request("", "", "", "", "", "", "", "");
		 int ind;
		 
	      while(! listNodeId.isEmpty())
	      {
	    	  ind = Integer.parseInt(listNodeId.get(0));	    	  
	    	  request = getNode(ind);
	    	  listNodeId.remove(0);
	    	  listNodeInfo.add(request);
	      }		 		 
		return listNodeInfo;		 
	 }
	 /*
	 public int numberCol(Request req){
		 String s=req.id;
		 TreeNode node = new TreeNode();
		return node.wigthNode();		 
	 }
	 */
	 
	 public int numberNodesAtLev(int lev){		 
		return treeIds.numberNodeAtLev(lev);	 
	 }
	 
	 public Request GetInfo(TreeNode node){
		Request req = new Request("", "", "", "", "", "", "", "");
		req = getNode(node.getUserObject());
		return req;
	 }
		 
	  public int wigthOfCol(Request req){
		  int id = Integer.parseInt(req.id);
		  TreeNode n = new TreeNode();
		  n = treeIds.getNodeById(id);	  
		return n.wigthNode();	  
	  }
	 
	  
	  public TreeNode updateTree(String idparent){
		  int idp = Integer.parseInt(idparent);
		  TreeNode parentN = new TreeNode();
		  parentN = treeIds.getNodeById(idp);
		  List <TreeNode> listch = new ArrayList<TreeNode>();
		  listch = parentN.ShowChildren(parentN);
	      while(! listch.isEmpty())
	      {	    	  
	    	  parentN.add(listch.get(0));
	    	  listch.remove(0);
	      }	
	      return treeIds;
	  }
	  
}
